package com.cos.shop.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

/**
 * @author: COS
 * @time: 2022/9/5 16:20
 * @description:
 */
public class DBUtils {
    private static DruidDataSource ds;
    private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();

    static {
        try {
            //加载配置文件
            Properties pro = new Properties();
            pro.load(DbUtils.class.getResourceAsStream("/jdbc.properties"));
            // 创建 数据源 数据库连接池
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/5 16:22
     * @description: 获取链接的方法
     */
    public static Connection getConnection() {
        //1 尝试获取链接
        Connection con = THREAD_LOCAL.get();
        try {
            //2 为空就创建
            if (con == null) {
                con = ds.getConnection();
                // 3 放入ThreadLocal当中
                THREAD_LOCAL.set(con);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //4 返回con
        return con;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/5 16:22
     * @description: 获取数据源的方法
     */
    public static DataSource getDataSource() {
        return ds;
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/9/5 16:25
     * @description: 关闭链接
     */
    public static void closeConnection(Connection con) {
        try {
            if (con != null) {
                con.close();
                //清空ThreadLocal
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/9/5 16:25
     * @description: 开启事务
     */
    public static void begin(){
        try {
            Connection con = getConnection();
            con.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/9/5 16:25
     * @description: 提交事务
     */
    public static void commit(){
        Connection con=null;
        try {
            con=getConnection();
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeConnection(con);
        }
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/9/5 16:25
     * @description: 回滚事务
     */
    public static void rollback(){
        Connection con=null;
        try {
            con=getConnection();
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeConnection(con);
        }
    }


    public static void main(String[] args) {

        System.out.println(new Date());
    }
}
